// todo: 服务器地址
var URL = "";

cc.VERSION = 20181111;
var HTTP = cc.Class({
  extends: cc.Component,

  statics: {
    sessionId: 0,
    userId: 0,
    master_url: URL,
    url: URL,
    sendRequest: function(path, data, handler, extraUrl) {
      var xhr = cc.loader.getXMLHttpRequest();
      xhr.timeout = 5000;
      var str = "?";
      for (var k in data) {
        if (str != "?") {
          str += "&";
        }
        str += k + "=" + data[k];
      }
      if (extraUrl == null) {
        extraUrl = HTTP.url;
      }
      var requestURL = extraUrl + path + encodeURI(str);
      console.log("RequestURL:" + requestURL);
      xhr.open("GET", requestURL, true);
      if (cc.sys.isNative) {
        xhr.setRequestHeader(
          "Accept-Encoding",
          "gzip,deflate",
          "text/html;charset=UTF-8"
        );
      }

      xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
          // add . 防止ios审核控制台出现充值字样
          try {
            var ret = JSON.parse(xhr.responseText);
            if (handler !== null) {
              handler(ret);
            } /* code */
          } catch (e) {
            console.log("err:" + e);
          } finally {
            // if (cc.vv && cc.vv.wc) {
            //   // cc.vv.wc.hide();
            // }
          }
        } else {
          console.log("status=" + xhr.readyState);
        }
      };

      if (cc.vv && cc.vv.wc) {
        //cc.vv.wc.show();
      }
      xhr.send();
      return xhr;
    }
  }
});
